<template>
  <div class="tinymce-editor">
    <editor
      v-model="internalValue"
      :init="init"
      :disabled="disabled"
      @change="handleChange"
      @onClick="onClick"
      v-if="!useMarkdown"
    />
  </div>
</template>

<script>
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/modern/theme'
import 'tinymce/plugins/image'
import 'tinymce/plugins/media'
import 'tinymce/plugins/table'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/contextmenu'
import 'tinymce/plugins/wordcount'
import 'tinymce/plugins/colorpicker'
import 'tinymce/plugins/textcolor'

import { staticBaseURL, uploadURL } from '@/env'
import { Emitter } from '@/mixins'

export default {
  mixins: [Emitter],
  components: {
    Editor
  },
  props: {
    useMarkdown: Boolean,
    value: {},
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default: 'lists image media table textcolor wordcount contextmenu'
    },
    toolbar: {
      type: [String, Array],
      default:
        'undo redo |  formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat'
    },
    validateEvent: Boolean
  },
  data () {
    return {
      // 初始化配置
      init: {
        language_url: '/static/editor/langs/zh_CN.js',
        language: 'zh_CN',
        skin_url: '/static/editor/skins/lightgray',
        height: 450,
        plugins: this.plugins,
        toolbar: this.toolbar,
        branding: false,
        menubar: false,
        images_upload_handler: (blobInfo, success, failure) => {
          var xhr, formData

          xhr = new XMLHttpRequest()
          xhr.withCredentials = false
          xhr.open('POST', uploadURL)

          xhr.onload = function () {
            var json

            if (xhr.status !== 200) {
              failure('HTTP Error: ' + xhr.status)
              return
            }
            json = JSON.parse(xhr.responseText)

            if (!json || json.result !== 0) {
              failure(this.$t('uploadError'))
              return
            }
            success(staticBaseURL + json.data)
          }

          formData = new FormData()
          formData.append('file', blobInfo.blob(), blobInfo.filename())

          xhr.send(formData)
        }
      },
      internalValue: this.value
    }
  },
  mounted () {
    tinymce.init({})
  },
  methods: {
    onClick (e) {
      this.$emit('onClick', e, tinymce)
    },
    clear () {
      this.internalValue = ''
    },
    handleChange () {
    },
    // ----------------------------------------------- MD -----------------------------------------------
    handleMdChange (val, render) {
      this.$emit('html', render)
    }
    // ----------------------------------------------- !MD -----------------------------------------------
  },
  watch: {
    value (newValue) {
      this.internalValue = newValue
    },
    internalValue (newValue) {
      this.$emit('input', newValue)
      if (this.validateEvent) {
        this.dispatch('ElFormItem', 'el.form.change')
      }
    }
  }
}
</script>
<style scoped lang="scss">
>>>.v-note-wrapper {
    min-height: 450px;
}
</style>
